<?php
/**
 *
 */
class Sx_article_mod extends CI_Model {

	var $r = false;

	/**
	 * 添加相关操作
	 */
	function addarticle($db, $tab) {
		if (!is_array($db)) {
			return $this -> r;
			exit ;
		}
		if (empty($tab)) {
			return $this -> r;
			exit ;
		}
		if ($this -> db -> insert($tab, $db) && $this -> db -> affected_rows() == 1) {
			$new_id_number = $this -> db -> insert_id();
			return $this -> r = $new_id_number;
		} else {
			return $this -> r;
		}
	}

	/**
	 * 获取最新帖子
	 */
	function getarticlelist($city, $cursor, $offset) {
		$mArr = array();
		//获取总条数
		if (!empty($city)) {
			$this -> db -> where("city", $city);
		}

		$n = $this -> articletotal($city);
		if ($cursor > $n) {
			$cursor = $n;
		}
		$this -> db -> limit($offset, $cursor);
		$this -> db -> order_by("pic_id", "desc");
		$Q = $this -> db -> get('sx_article');
		if ($Q -> num_rows() > 0) {
			foreach ($Q->result_array() as $row) {
				$row['comment'] = $this -> getcomment($row['pic_id'], "", "");
				$filename = substr($row['pic_path'], strrpos($row['pic_path'], "/") + 1);
				if (empty($row['pic_thumb'])) {
					$pic_thumb = substr($filename, 0, -4) . "_thumb.jpg";
					$row['pic_thumb'] = base_url() . "upload/thumb/" . $pic_thumb;
				}

				$mArr[] = $row;
			}

			return $mArr;
		}
		$Q -> free_result();
	}

	/**
	 * 获取帖子总数
	 */
	function articletotal($city = "") {
		if (!empty($city)) {
			$this -> db -> where('city', $city);
		}
		//$this -> db -> where('city', $city);
		$this -> db -> order_by("pic_id", "desc");
		$Q = $this -> db -> get('sx_article');
		if ($Q -> num_rows() > 0) {
			return $Q -> num_rows();
		} else {
			return 0;
		}
		$Q -> free_result();

	}

	/**
	 * 获取评论总条数
	 */
	function commnettotal($pic_id) {
		$this -> db -> where('pic_id', $pic_id);
		$Q = $this -> db -> get('sx_comment');
		if ($Q -> num_rows() > 0) {
			return $Q -> num_rows();
		} else {
			return 0;
		}
		$Q -> free_result();
	}

	/**
	 * 获取帖子的评论
	 */

	function getcomment($pic_id, $cursor = "", $offset = "") {
		$dbarr = array();
		if (!isset($pic_id) || empty($pic_id)) {
			return "";
			exit ;
		}
		$this -> db -> select("sx_comment.c_id, sx_comment.pic_id, sx_comment.author, sx_comment.c_time, sx_comment.content, sx_comment.c_status, sx_users.user_nicename");
		$this -> db -> from('sx_comment');
		$this -> db -> where("sx_comment.pic_id", $pic_id);
		$this -> db -> join('sx_users', 'sx_users.ID=sx_comment.author');
		$this -> db -> order_by("sx_comment.c_id", "ASC");
		if (!empty($cursor) && !empty($offset)) {
			$this -> db -> limit($offset, $cursor);
		}
		$Q = $this -> db -> get();
		if ($Q -> num_rows() > 0) {
			foreach ($Q->result_array() as $row) {
				$dbarr[] = $row;
			}
			if (empty($cursor) && empty($offset)) {
				$dbarr['total'] = $this -> commnettotal($pic_id);
			}

			return $dbarr;
		} else {
			return "";
		}
		$Q -> free_result();
	}

	/*添加评价*/
	function setappraise($pic_id, $p) {
		$sql = "update sx_article set {$p}={$p}+1 where pic_id= {$pic_id}";
		$Q = $this -> db -> query($sql);
		if ($this -> db -> affected_rows()) {
			return TRUE;
		} else {
			return FALSE;
		}
		$Q -> free_result();
	}

	//---------------------------------------------------------------------------------------------
	/**
	 * 获取最近距离
	 */
	//---------------------------------------------------------------------------------------------
	function getnear($x, $y, $distance, $cursor, $offset, $city, $n = FALSE) {
		$mArr = array();
		if (!empty($city)) {
			$this -> db -> where("city", $city);
		}
		
		$this -> db -> order_by("pic_id", "desc");
		
		$Q = $this -> db -> get('sx_article');
		
		if(empty($x) || empty($y)){
			//$this -> db -> limit($offset, $cursor);
            $this->db->limit((int)$offset, (int)$cursor);
		}
		if ($Q -> num_rows() > 0) {
			foreach ($Q->result_array() as $row) {

				if (empty($x) || empty($y)) {
					$row['comment'] = $this -> getcomment($row['pic_id'], "", "");
					if (empty($row['pic_thumb'])) {
						$filename = substr($row['pic_path'], strrpos($row['pic_path'], "/") + 1);
						$pic_thumb = substr($filename, 0, -4) . "_thumb.jpg";
						$row['pic_thumb'] = base_url() . "upload/thumb/" . $pic_thumb;
					}

				} else {
					$m = $this -> CaculateDistance($x, $y, $row['pic_x'], $row['pic_y']);

                    $row['N'] = $m;
					if ($m && $m < $distance) {
						$row['comment'] = $this -> getcomment($row['pic_id'], "", "");
						if (empty($row['pic_thumb'])) {
							$filename = substr($row['pic_path'], strrpos($row['pic_path'], "/") + 1);
							$pic_thumb = substr($filename, 0, -4) . "_thumb.jpg";
							$row['pic_thumb'] = base_url() . "upload/thumb/" . $pic_thumb;
						}
					}

				}

				$mArr[] = $row;
			}
			if ($n) {
				return count($mArr);
				exit ;
			} else {
				if(!empty($x)){
					if ($offset > count($mArr)) {
					    $offset = count($mArr);
				    }
                    $mArr =$this->array_multisort_by_name($mArr,"N");


				return array_slice($mArr, $cursor, $offset);
					
				}else{
					//return $mArr;
                    return array_slice($mArr, $cursor, $offset);
				}
				
			}

		} else {
			return "";
		}
		$Q -> free_result();
	}

	/*计算距离*/
	function CaculateDistance($lat1, $lng1, $lat2, $lng2) {
		if (empty($lng1) || empty($lat1)) {
			return FALSE;
			exit ;
		}
		//其中Lat表示纬度，Lng表示经度
		$PI = 3.1415926535;
		$EARTH_RADIUS = 6378.137;
		// 地球半径
		$radLat1 = $lat1 * $PI / 180;
		// 转化为弧度值
		$radLat2 = $lat2 * $PI / 180;
		$a = $radLat1 - $radLat2;
		$b = ($lng1 - $lng2) * $PI / 180;
		$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
		$s = $s * $EARTH_RADIUS;
		$s = round($s * 10000);
		return $s;
	}

	/**
	 * 获取最近最好的
	 */
	function getindextasty($x, $y, $distance = "", $cursor, $offset, $city, $n = FALSE) {
		$mArr = array();
		$dingwei = TRUE;
		if (!empty($city)) {
			$this -> db -> where("city", $city);
		}
		/*如果未定位*/
		if (empty($x) || empty($y)) {
			$this -> db -> limit($offset, $cursor);
			$dingwei = FALSE;
		}

		$this -> db -> order_by("tasty", "desc");
		$Q = $this -> db -> get('sx_article');
		if ($Q -> num_rows() > 0) {

			foreach ($Q->result_array() as $row) {

				if (!$dingwei) {
					$row['comment'] = $this -> getcomment($row['pic_id'], "", "");

					if (empty($row['pic_thumb'])) {
						$filename = substr($row['pic_path'], strrpos($row['pic_path'], "/") + 1);
						$pic_thumb = substr($filename, 0, -4) . "_thumb.jpg";
						$row['pic_thumb'] = base_url() . "upload/thumb/" . $pic_thumb;
					}

					$mArr[] = $row;
				} else {
					$m = $this -> CaculateDistance($x, $y, $row['pic_x'], $row['pic_y']);
					if ($m && $m < $distance) {
						$row['N'] = $m;
						$row['comment'] = $this -> getcomment($row['pic_id'], "", "");
						$filename = substr($row['pic_path'], strrpos($row['pic_path'], "/") + 1);
						$pic_thumb = substr($filename, 0, -4) . "_thumb.jpg";
						$row['pic_thumb'] = base_url() . "upload/thumb/" . $pic_thumb;
						$mArr[] = $row;
					}
				}

			}
			if ($n) {
				/*返回总条数*/
				return count($mArr);
				exit ;
			} else {
				/*如果未定位*/
				if (empty($x) || empty($y)) {
					return $mArr;
				} else {
					/*如果定位*/
					if ($offset > count($mArr)) {
						$offset = count($mArr);
					}

					return array_slice($mArr, $cursor, $offset);
				}

			}

		} else {
			return "";
		}
		$Q -> free_result();
	}

    function array_multisort_by_name($fromarr,$key,$sort=SORT_ASC){
        if(is_array($fromarr)){
            foreach($fromarr as $k=>$v){
                if(is_array($v)){
                    $key_arr[]=$v[$key];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arr,$sort,$fromarr);
        return $fromarr;
    }

}
?>